package com.lyf.scm.plan.domain.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.plan.api.dto.DirectlyExecuteDTO;
import com.lyf.scm.plan.domain.convertor.DirectlyExeSelectConvertor;
import com.lyf.scm.plan.domain.convertor.DirectlyExecuteConvertor;
import com.lyf.scm.plan.insfrastructure.db.dataobject.DirectlyExecuteDO;
import com.lyf.scm.plan.insfrastructure.db.dataobject.bigdata.DirectlyExecuteInDO;
import com.lyf.scm.plan.insfrastructure.db.dataobject.bigdata.DirectlyExecuteUpDO;
import com.lyf.scm.plan.insfrastructure.db.mapper.DirectlyExecuteMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * description:
 * 直营预测计划--执行
 *
 * @author: haodong
 * @data: 2020/8/5 13:34
 */
@Service
public class DirectlyExecuteRepository {

    @Resource
    private DirectlyExecuteConvertor directlyExecuteConvertor;
    @Resource
    private DirectlyExeSelectConvertor directlyExeSelectConvertor;
    @Resource
    private DirectlyExecuteMapper directlyExecuteMapper;

    /**
     * 按条件分页查询直营预测计划
     *
     * @param salesPlanDirectlyExecute
     * @return
     */
    public PageInfo<DirectlyExecuteDTO> listByPage(DirectlyExecuteDTO salesPlanDirectlyExecute) {
        PageInfo<DirectlyExecuteDO> pageInfo = PageHelper.startPage(salesPlanDirectlyExecute.getPageNum(), salesPlanDirectlyExecute.getPageSize()).doSelectPageInfo(
                () -> directlyExecuteMapper.listByPage(directlyExeSelectConvertor.dtoToDO(salesPlanDirectlyExecute)));
        return directlyExecuteConvertor.doToDTO(pageInfo);
    }

    /**
     * 按条件查询直营预测计划
     *
     * @param salesPlanDirectlyExecute
     * @return
     */
    public List<DirectlyExecuteDTO> listBySelective(DirectlyExecuteDTO salesPlanDirectlyExecute) {
        return directlyExecuteConvertor.doToDTO(
                directlyExecuteMapper.listBySelective(directlyExecuteConvertor.dtoToDO(salesPlanDirectlyExecute)));
    }

    /**
     * 查询全部直营预测计划
     *
     * @param salesPlanDirectlyExecute
     * @return
     */
    public List<DirectlyExecuteDTO> listAllByDays(DirectlyExecuteDTO salesPlanDirectlyExecute) {
        return directlyExecuteConvertor.doToDTO(
                directlyExecuteMapper.listAllByDays(directlyExeSelectConvertor.dtoToDO(salesPlanDirectlyExecute)));
    }

    /**
     * 批量新增
     *
     * @param directlyExecuteInDOs
     * @return void
     **/
    public void batchInsert(List<DirectlyExecuteInDO> directlyExecuteInDOs) {
        directlyExecuteMapper.batchInsert(directlyExecuteInDOs);
    }

    /**
     * 直营预测——执行 根据门店和日期获取所有需要更新的数据（默认为13天数据，今日起）
     *
     * @param code
     * @param startDate
     * @param endDate
     * @return {@link List<DirectlyExecuteDO>}
     **/
    public List<DirectlyExecuteUpDO> listUpByStoreAndDate(String code, Date startDate, Date endDate) {
        return directlyExecuteMapper.listUpByStoreAndDate(code, startDate, endDate);
    }

    /**
     * 删除当前日期之后的所有数据
     *
     * @param startDate
     * @return void
     **/
    public void batchDeleteByStartDate(String storeCode, Date startDate) {
        directlyExecuteMapper.batchDeleteByStartDate(storeCode, startDate);
    }

    public void delete(String opSign) {
        directlyExecuteMapper.delete(opSign);
    }

    public void rmByIds(List<Long> ids) {
        directlyExecuteMapper.rmByIds(ids);
    }

    public void upByIds(List<DirectlyExecuteUpDO> list) {
        directlyExecuteMapper.upByIds(list);
    }

    public void deleteByOpSign(String opSign) {
        directlyExecuteMapper.deleteByOpSign(opSign);
    }
}
